package priv.dajie.tzzq.common.enumTypeHandler;

import java.util.Arrays;
import java.util.Optional;

/**
 * @ClassName BaseEnum
 * @Description
 * @Author dajie
 * @Date 2019/10/16 4:23 下午
 * @Version V1.0
 **/
public interface BaseEnum<T> {

    T getDictKey();

    default T getValue(){
        return this.getDictKey();
    }

    static <T> BaseEnum valueOfEnum(Class<BaseEnum> enumClass, T value){
        if (value == null){
            return null;
        }
        // 获取枚举
        BaseEnum[] enums = enumClass.getEnumConstants();
        // 查找出value对应的枚举
        Optional<BaseEnum> optional = Arrays.asList(enums).stream().filter(baseEnum -> baseEnum.getDictKey().equals(value)).findAny();
        if (optional.isPresent()){
            return optional.get();
        }
        throw new RuntimeException("未找到：" + value + "对应的" + enumClass.getName());
    }

}
